package everydayGrade.t4;

import org.junit.jupiter.api.Test;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @Author: Siantar
 * @Date: 2023-07-18-15:53
 * @Description: 1.0
 */
public class T1209 {
    public int index = 0;

    public String removeDuplicates(String s, int k) {
        int n = s.length();
        Deque<int[]> deque = new LinkedList<>();
        while (index < n) {
            int[] p = getNext(s);
            p[1] %= k;
            System.out.println((char) p[0] + " " + p[1]);
            if (!deque.isEmpty()) {
                int[] pl = deque.peek();
                if (pl[0] == p[0]) {
                    deque.pop();
                    p[1] = (pl[1] + p[1]) % k;
                }
            }
            if (p[1] != 0) deque.push(p);
        }
        String res = "";
        while (!deque.isEmpty()) {
            int[] p = deque.pop();
            for (int i = 0; i < p[1]; i++) {
                res = (char) p[0] + res;
            }
        }
        return res;
    }

    public int[] getNext(String s) {
        int c = s.charAt(index);
        int sum = 1;
        index++;
        while (index < s.length() && s.charAt(index) == c) {
            sum++;
            index++;
        }
        return new int[]{c, sum};
    }

    @Test
    public void test() {
        System.out.println(removeDuplicates("deeedbbcccbdaa", 3));
    }
}
